package com.qhm123.spark.douban;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.Link;
import com.google.gdata.data.Person;
import com.google.gdata.data.douban.Attribute;
import com.google.gdata.data.douban.SubjectEntry;
import com.google.gdata.data.douban.SubjectFeed;
import com.google.gdata.data.douban.Tag;
import com.google.gdata.util.ServiceException;
import com.qhm123.spark.model.Book;
import com.qhm123.spark.model.DbHelper;

public class demo {

	private static void javaClientDouban() {
		String apiKey = "046a0792404735c00d3e16b6c0d5a140";
		String secret = "4be055fdab5fe0d5";
		DoubanService myService = new DoubanService("ApplicationName", apiKey,
				secret);
		myService.getAuthorizationUrl(null);
		try {
			SubjectFeed feed = myService.findBook(null, "计算机", 1, 10);
			for (SubjectEntry se : feed.getEntries()) {
				String id = se.getId().substring(
						se.getId().lastIndexOf('/') + 1);
				System.out.println(id);
				System.out.println(se.getTitle().getPlainText());

				Book b = new Book();
				b.id = id;
				b.title = se.getTitle().getPlainText();

				SubjectEntry book = myService.getBook(id);
				System.out.println("Summary: "
						+ book.getSummary().getPlainText());

				b.summary = book.getSummary().getPlainText();

				List<Link> links = book.getLinks();
				for (Link link : links) {
					if (link.getRel().equals("image")) {
						System.out.println(link.getHref() + ": "
								+ link.getRel());
						b.imageurl = link.getHref();
					}
				}

				List<Person> authors = se.getAuthors();
				for (Person author : authors) {
					System.out.println("author: " + author.getName());

					b.author = author.getName();
				}

				List<Tag> tags = book.getTags();
				System.out.println("tags len: " + tags.size());
				for (Tag tag : tags) {
					System.out.println(tag.getName() + ": " + tag.getCount());
				}

				List<Attribute> attrs = se.getAttributes();
				for (Attribute attr : attrs) {
					System.out.println(attr.getName() + ": "
							+ attr.getContent());
					if (attr.getName().equals("isbn10")) {
						b.isbn = attr.getContent();
					} else if (attr.getName().equals("translator")) {
						b.translator = attr.getContent();
					} else if (attr.getName().equals("price")) {
						b.price = attr.getContent();
					} else if (attr.getName().equals("publisher")) {
						b.publisher = attr.getContent();
					} else if (attr.getName().equals("pubdate")) {
						b.pubdate = attr.getContent();
					}
				}

				DbHelper.getInstance().putBook(b);
			}
		} catch (IOException e) {
			System.out.println(e.getStackTrace());
		} catch (ServiceException e) {
			System.out.println(e.getStackTrace());
		}
	}

	public static void main(String[] args) {
		// search("计算机", 1, 10);
		javaClientDouban();
	}

	public static List<Book> search(String value, int startIndex, int limit) {
		DefaultHttpClient client = new DefaultHttpClient();
		String res = "";
		try {
			HttpGet httpost = null;
			httpost = new HttpGet(new URI(
					"http://api.douban.com/book/subjects?alt=json&q=" + value));
			HttpResponse response = client.execute(httpost);
			res = EntityUtils.toString(response.getEntity());
			// System.out.println(res);
		} catch (Exception e) {
		}

		try {
			JSONObject json = new JSONObject(res);
			return parseBooksJSON(json);
		} catch (JSONException e) {
			return null;
		}
	}

	public static List<Book> parseBooksJSON(JSONObject json)
			throws JSONException {
		List<Book> books = new ArrayList<Book>();
		JSONArray entries = json.getJSONArray("entry");
		for (int i = 0; i < entries.length(); i++) {
			JSONObject obj = entries.getJSONObject(i);
			Book book = parseBookJSON(obj);
			books.add(book);
		}

		return books;
	}

	public static Book parseBookJSON(JSONObject object) throws JSONException {
		Book book = new Book();
		book.id = object.getJSONObject("id").getString("$t");
		book.title = object.getJSONObject("title").getString("$t");
		System.out.println(book.toString());

		StringBuilder sb = new StringBuilder();
		JSONArray translators = object.getJSONArray("translator");
		for (int i = 0; i < translators.length(); i++) {
			sb.append(translators.get(i));
			if (i != translators.length() - 1) {
				sb.append("/");
			}
		}
		book.translator = sb.toString();

		sb = new StringBuilder();
		JSONArray authors = object.getJSONArray("author");
		for (int i = 0; i < authors.length(); i++) {
			sb.append(authors.get(i));
			if (i != authors.length() - 1) {
				sb.append("/");
			}
		}
		book.author = sb.toString();
		book.summary = object.getString("summary");

		try {
			book.subtitle = object.getString("subtitle");
		} catch (JSONException e) {
		}

		return book;
	}
}
